package cn.candy.user.controller;

import cn.candy.basic.exception.BusinessExcpetion;
import cn.candy.basic.util.AjaxResult;
import cn.candy.user.dto.LoginDto;
import cn.candy.user.service.ILogininfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;


/**
 * 登录接口类
 */
@RestController
@RequestMapping("/login")
@Api(tags = "登录接口类",description = "登录接口详细描述")
public class LoginController {

    @Autowired
    private ILogininfoService logininfoService;

    /**
     * 账号登录
     * @param loginDto - 传入登录传递对象
     * @return
     */
    @PostMapping("/account")
    @ApiOperation(value = "账号登录接口",notes = "")
    public AjaxResult accountLogin(@RequestBody LoginDto loginDto){
        try {
            return logininfoService.accountLogin(loginDto);
        } catch (BusinessExcpetion e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("登录失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误，" + e.getMessage());
        }
    }


    /**
     * 微信登录
     * @param map - 传入登录传递对象
     * @return
     */
    @PostMapping("/wxUser")
    @ApiOperation(value = "微信登录接口",notes = "")
    public AjaxResult wxUserLogin(@RequestBody Map<String,String> map){
        try {
            return logininfoService.wxUserLogin(map);
        } catch (BusinessExcpetion e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("登录失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误，" + e.getMessage());
        }
    }


    /**
     * 微信绑定
     * @param map - 传入登录传递对象
     * @return
     */
    @ApiOperation(value = "微信绑定接口",notes = "")
    @PostMapping("/binder/wechat")
    public AjaxResult wxUserBinder(@RequestBody Map<String,String> map){
        try {
            return logininfoService.wxUserBinder(map);
        } catch (BusinessExcpetion e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("绑定失败，" + e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMsg("系统错误，" + e.getMessage());
        }
    }

}
